<!--头部公共部分-->
{include file="common/header" /}
<div id="wodekehu" class="x-body" class="clear:both;">
    <!--<div class="site-demo-button" style="margin-bottom: 0;" lay-filter="selBtn">-->
        <!--<button class="layui-btn site-demo-active" id="course">新增上课时间</button>-->
        <!--<button class="layui-btn site-demo-active layui-btn-warm" id="course_package">新增常用时间段</button>-->
    <!--</div>-->
    <div class="layui-row"
         style="padding-left:20px;border-left:1px solid #e5e5e5;border-right:1px solid #e5e5e5;">
        <div class="layui-form layui-col-md12 x-so layui-form-pane">
            <div class="layui-input-inline">
                <button class="layui-btn" id="table-add">
                    <i class="layui-icon layui-icon-add-circle-fine"></i>新增上课时间
                </button>
            </div>
        </div>
    </div>
    <table class="layui-table" id="datalist" lay-filter="Event"></table>
    <script type="text/html" id="barTable">
        <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="edit">修改</a>
        <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="configure">配置</a>
        <!--<a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="follow_up">删除</a>-->
    </script>
</div>
<!--底部公共部分-->
{include file="common/footer" /}
<script>
    layui.use(['table', 'element', 'form', 'laydate', 'layer'], function () {
        var table = layui.table,
            laydate = layui.laydate,
            element = layui.element,
            form = layui.form,
            layer = layui.layer,
            $ = layui.$;

        var loading = layer.load();

        $('#table-add').click(function () {
            layer.open({
                type: 2,
                skin: 'layui-layer-lan',
                closeBtn: 2,
                title: '新增上课时间',
                area: ['60%', '50%'],
                shadeClose: false,
                content: "{:url('Schooltime/add')}",
            });
        });

        //日期
        laydate.render({
            elem: '#start',
            type: 'date'
            ,trigger: 'click'
        });

        //方法级渲染
        var tableIns = table.render({
            elem: '#datalist'
            , url: '{:url("Schooltime/lists")}'
            , method: 'POST'
            // , toolbar: 'default' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
            , loading: true
            , parseData: function (res) { //res 即为原始返回的数据
                if (res.code === 1) {
                    res.code = 0;
                } else {
                    res.code = -1;
                }
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.data //解析数据列表
                };
            },
            cols: [[
                {field: 'week', title: '节数', align: 'center'},
                {field: 'campus_name', title: '校区', align: 'center'},
                {field: 'start_time', title: '开始时间', align: 'center', edit: 'text'},
                {field: 'end_time', title: '结束时间', align: 'center', edit: 'text'},
            ]]
            , height: 'full-180'
            , page: true
            , done: function () {
                layer.close(loading);
            }
        });
        table.on('edit(Event)', function (obj) {
            var value = obj.value //得到修改后的值
                , data = obj.data //得到所在行所有键值
                , field = obj.field; //得到字段
            if (field === 'end_time') {
                $.post('{:url("Schooltime/edit")}'
                , {
                    'id': data.id,
                    'start_time': data.start_time,
                    'end_time': value
                }
                , function (result) {
                        layer.msg(result.msg);
                    if (result.code === 1) {
                        tableIns.reload();
                    }
                });
            }else if (field === 'start_time') {
                $.post('{:url("Schooltime/edit")}'
                    , {
                        'id': data.id,
                        'end_time': data.end_time,
                        'start_time': value
                    }
                    , function (result) {
                        layer.msg(result.msg);
                        if (result.code === 1) {
                            tableIns.reload();
                        }
                    });
            }
        });

    });
</script>

